package com.youga.xuapplication.util;


import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
import android.util.Base64;

import com.youga.xuapplication.config.Config;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;


/**
 * @author Youga
 */
public class SpUtil {
    private static String TAG = "SpUtil";

    private SpUtil() {
    }

    private static SharedPreferences getSharedPreferences(Context context) {
        return context.getSharedPreferences(Config.SPF_NAME, Context.MODE_PRIVATE);
    }


    public static <T> void setDataSharedPreferences(Context context, String key, T value) {
        Editor editor = getSharedPreferences(context).edit();
        if (value instanceof String) {
            editor.putString(key, (String) value);
        } else if (value instanceof Long) {
            editor.putLong(key, (Long) value);
        } else if (value instanceof Integer) {
            editor.putInt(key, (Integer) value);
        } else if (value instanceof Boolean) {
            editor.putBoolean(key, (Boolean) value);
        }
        editor.apply();
    }

    public static String getStringSharedPreferences(Context context, String key) {
        return getSharedPreferences(context).getString(key, "");
    }

    public static boolean getBooleanSharedPreferences(Context context, String key) {
        return getSharedPreferences(context).getBoolean(key, false);
    }

    public static void removeSharedPreferences(Context context, String key) {
        Editor editor = getSharedPreferences(context).edit();
        editor.remove(key);
        editor.apply();
    }

    public static void removegetSharedPreferences(Context context, String... key) {
        if (key.length > 0) {
            for (int i = 0; i < key.length; i++) {
                removeSharedPreferences(context, key[i]);
            }
        }
    }

    public static <T> void putEntityPreferences(Context context, T entity) {
        Editor editor = getSharedPreferences(context).edit();
        try {
            ByteArrayOutputStream toByte = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(toByte);
            oos.writeObject(entity);
            // 对byte[]进行Base64编码
            String obj64 = new String(Base64.encode(toByte.toByteArray(), Base64.DEFAULT));
            editor.putString(entity.getClass().getSimpleName(), obj64);
            editor.apply();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("unchecked")
    public static <T> T getEntityPreferences(Context context, Class clazz) {
        SharedPreferences sp = getSharedPreferences(context);
        try {
            String obj64 = sp.getString(clazz.newInstance().getClass().getSimpleName(), "");
            if (TextUtils.isEmpty(obj64)) {
                return null;
            }
            byte[] base64Bytes = Base64.decode(obj64, Base64.DEFAULT);
            ByteArrayInputStream stream = new ByteArrayInputStream(base64Bytes);
            ObjectInputStream ois = new ObjectInputStream(stream);
            return (T) ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void cleanDataSharedPreferences(Context context) {
//        SharedPreferences preferences = getSharedPreferences(context);
//        String perUsername = preferences.getString(LoginActivity.PRE_USERNAME, "");
//        String perCompanyId = preferences.getString(LoginActivity.PRE_COMPANYID, "");
//        Editor editor = preferences.edit();
//        editor.clear();
//        editor.putString(LoginActivity.PRE_USERNAME, perUsername);
//        editor.putString(LoginActivity.PRE_COMPANYID, perCompanyId);
//        editor.putString(Config.SAVE_VERSION, Config.NEED_TO_WELCOME_VERSION);
//        editor.apply();
    }
}
